<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class Home_IndexController extends Cms_Controller_Action
{
	public function init()
	{
	  $this->view->siteTitle = 'EmotionsMirror';	
			
	}
	public function indexAction()
	{
	  	$id = $this->_getParam('id');
		if(isset($_SESSION["sess_userid"])!="")
		{
		$this->_redirect('/expressions/index/inbox/1');
		}
		if($id == 'invalid')
		{
			$this->view->message = "Invalid username or password";
		} else {
			$this->view->message = "";
		}
	}
	public function loginAction() 
	{	
	    if($_SESSION["sess_userid"]!="")
		{
		$this->_redirect('/expressions/index/inbox/1');
		}
		else
		{      
		$email=$_POST['email'];
		//echo $username;exit;
		$password=md5($_POST['password']);
		$log = Doctrine::getTable('Users')->loginCheck($email,$password);
		if($log->numCount>0)
		{
		//store values in session
		$users= Doctrine::getTable('Users')->getUserId($email);
		//echo '<pre>';print_r($Users);exit;
		$_SESSION['start'] = time();
		$_SESSION['expire'] = $_SESSION['start'] + (30* 60) ; //session expires in 15 minutes
		 $_SESSION["sess_userid"]=$users['user_id'];
		 //echo $_SESSION["sess_profilephoto"];exit;
		 $_SESSION["sess_firstname"]=$users['first_name'];
		 $_SESSION["sess_username"]=$users['username'];
		 //echo $_SESSION["sess_username"];exit;
		 $_SESSION["sess_email"]=$users['email'];
		 $userdetails= Doctrine::getTable('UserDetails')->getUserDetails($_SESSION["sess_userid"]);
		 //echo '<pre>';print_r($userdetails);exit;
		  $_SESSION["sess_photo"]=$userdetails['photo'];
		//echo $_SESSION["sess_photo"];exit;        
		 //echo "you are successfully lgin into ur account";
		 $this->_redirect("/expressions/index/inbox/1");
		 
		}
		else {
			$this->_redirect("/home/index/index/invalid");
			}
		}
		
	}
	public function registerSaveAction() 
	{	
	   	//echo '<pre>';print_r($_POST);exit;
		$username=$_POST['username'];
		//echo $username;exit;
		$password=md5($_POST['password']);
		$_POST['password']=$password;
		$result = Doctrine::getTable('Users')->validateUser($username);
		//echo '<pre>';print_r($result);exit;
		if($result['numCount']>0)
		{
			$this->_redirect("/home/index/register/invalid");
		}
		else
		{
			$result = Doctrine::getTable('Users')->addUser($_POST);
			$this->_redirect("/home/index/index/");
			
		}
		
		
		
	}
	public function registerAction()
	{
		$id = $this->_getParam('id');
		$message="";
		if($id=="invalid")
		{
			$message="username already exist";
		}
		$this->view->message=$message;
	}
	public function changePasswordAction()
	{
		$msg="";
		$memid=base64_decode($this->_getParam('id'));
		//echo $memid;exit;
		if($_POST)
		{
			//echo $memid;exit;
			//echo '<pre>';print_r($_POST);exit;
			$oldpwd=md5($_POST['oldpwd']);
			$userpwd=Doctrine::getTable('Users')->getPassword($memid);
			//echo $oldpwd."andd...............".$userpwd['password'];exit;
			if($oldpwd==$userpwd['password'])
			{
				//echo "update";exit;
				$new=Doctrine::getTable('Users')->updatePassword(md5($_POST['newpwd']),$memid);
				//echo $new;exit;
				if($new!=0)
				{
					$msg="Password changed successfully";
				}
			}
			else
			{
					$msg="Password do not match please try again";
			}
			//$email=$_POST['email'];
		}
		$this->view->msg=$msg;
		$this->view->memid=$memid;
		
	}
	public function forgotPasswordAction()
	{
		global $forgotPasswordSubject,$frogotPasswordMessage;
		$msg="";
		include('../lib/mail/SendEmail.php');
		if($_POST)
		{
				//echo '<pre>';print_r($_POST);exit;
				$email=$_POST['email'];
				if($email!="")
				{
					$uniqueRecard=Doctrine::getTable('Users')->checkUniqueRecord($email);
					//echo $uniqueRecard->numCount;exit;
					if($uniqueRecard['numCount'] > 0)
					{
						$arr=Doctrine::getTable('Users')->getMembersByMailId($email);
						$user_id=$arr['user_id'];
						//echo $user_id;
						$pwdlength = 10;
						$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
						$newpass ='';    
						for ($pwd = 0; $pwd < $pwdlength; $pwd++) {
							$newpass .= $characters[mt_rand(0, strlen($characters))];
						}
						$result=Doctrine::getTable('Users')->updatePassword(md5($newpass),$email);
						//echo $result;exit;
					//mysql_query("update members set password='".md5($newpass)."' where email='$email'");	
						$mailto=$email;
						$mailsubject= $forgotPasswordSubject;
						$Body = str_replace("<FULLNAME>",$arr['first_name'], $frogotPasswordMessage);
						$mailBody = str_replace("<PASSWORDLINK>", $passwordLink, $Body);
						$mailbody="Dear ".($arr['username']).",<br>
									Please find your new password.
									<br>
									".$newpass."
									<br>
									Please change your password after login.
									<br>

									Emotions Mirror Team
									 ";
							if(sendMail($mailto,$mailsubject,$mailbody,''))
							{
							$msg="Your password is reset and sent to your email";
							}

					}
					else
					{
						$msg="Please Enter Valid Email";
					}

				}
			else
			{
			$msg="Please Enter Email";
			}
		}
		$this->view->msg=$msg;
	}
	public function fgtpassAction()
	{
		
	}
	
	public function notificationsAction()
	{
		include('../lib/mail/SendEmail.php');
		$memid=$_SESSION["sess_userid"];
		//$trigger_notifications= Doctrine::getTable('Notifications')->getNotifications($memid);
		$trigger_ids= Doctrine::getTable('Comments')->getAllTriggerIds();
		foreach($trigger_ids as $trigger_ids)
		{
			$owner_ids = Doctrine::getTable('TriggersRelations')->getOwnerDetailsByTriggerId($trigger_ids['trigger_id']);
			$owner_id = array_unique($owner_ids);
			//echo'<pre>';print_r($owner_id);exit;
			if($owner_id['owner_id']!=$memid)
			{
				$notifications = Doctrine::getTable('Notifications')->getNotifications($owner_id['owner_id']);
				//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
				if($notifications['mytrigger_comments'] !="off")
				{
					$user_name = Doctrine::getTable('Users')->getEmail($owner_id['owner_id']);
					$loginLink = $this->CONFIG->serverAddress.'home';
					$Subject = "comment on trigger notification from EmotionsMirror";
					$Message = 'Hello "'.$user_name['first_name'].'",<br/><br/>you have Comment Notification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
					global $serverAddress;
					sendMail($user_name['email'],$Subject,$Message,$_SESSION["sess_email"]);
				}
			}
			$users = Doctrine::getTable('TriggersRelations')->getUsersDetailsByTriggerId($trigger_id);
			//echo'<pre>';print_r($users);exit;
			foreach($users as $ids)
			{
				//echo'<pre>';print_r($ids['user_id']);exit;
				$notifications = Doctrine::getTable('Notifications')->getNotifications($ids['user_id']);
				//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
				if($notifications['comments_others_shared'] !="off")
				{
				$user_name = Doctrine::getTable('Users')->getEmail($ids['user_id']);
				//echo'<pre>';print_r($user_name);exit;
				$loginLink = $this->CONFIG->serverAddress.'home';
				$Subject = "comment on trigger notification from EmotionsMirror";
				$Message = 'Hello "'.$user_name['first_name'].'",<br/><br/>you have Comment Notification  in your emotionmirror account<br/><br/>
							<br/><br/><br/>Please click on the following link to login into your account<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
							Emirror team';
				global $serverAddress;
				sendMail($user_name['email'],$Subject,$Message,$_SESSION["sess_email"]);
				}
			}
			$result=Doctrine::getTable('Comments')->updateCommentSendMailStatus($trigger_ids['id']);		
		}
		/////////// send mails to shared triggers##########################################
		$all_triggers = Doctrine::getTable('TriggersRelations')->getAllTriggersByUserId($memid);
		foreach($all_triggers as $all_triggers)
		{
			$notifications = Doctrine::getTable('Notifications')->getNotifications($all_triggers['user_id']);
			if($notifications['others_shared_triggers'] !="off")
			{
				$user_name = Doctrine::getTable('Users')->getEmail($all_triggers['user_id']);
				$loginLink = $this->CONFIG->serverAddress.'home';
				$Subject = "Trigger notification from EmotionsMirror";
				$Message = 'Hello "'.$user_name['user_name'].'",<br/><br/>you have TriggerNotification  in your emotionmirror  <br/><br/>
							<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
							Emirror team';
				global $serverAddress;
				sendMail($user_name['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
				$result=Doctrine::getTable('TriggerRelations')->updateTriggerSendMailStatus($all_triggers['id']);
			}
		}
		/////send mails to album users based on notificationssssss########################################
		
		$Photo_ids= Doctrine::getTable('EmComments')->getAllPhotoIds();
		foreach($Photo_ids as $Photo_ids)
		{
			$owner_ids = Doctrine::getTable('PhotosRelations')->getOwnerDetailsByPhotoId($Photo_ids['Photo_id']);
			$owner_id = array_unique($owner_ids);
			//echo'<pre>';print_r($owner_id);exit;
			if($owner_id['owner_id']!=$memid)
			{
				$notifications = Doctrine::getTable('AlbumNotifications')->getNotifications($owner_id['owner_id']);
				//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
				if($notifications['myphotos_comments'] !="off")
				{
					$user_name = Doctrine::getTable('Users')->getEmail($owner_id['owner_id']);
					$loginLink = $this->CONFIG->serverAddress.'home';
					$Subject = "comment on album notification from EmotionsMirror";
					$Message = 'Hello "'.$user_name['first_name'].'",<br/><br/>you have Comment Notification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
					global $serverAddress;
					sendMail($user_name['email'],$Subject,$Message,$_SESSION["sess_email"]);
				}
			}
			$users = Doctrine::getTable('PhotoRelations')->getUsersDetailsByPhotoId($Photo_ids['Photo_id']);
			//echo'<pre>';print_r($users);exit;
			foreach($users as $ids)
			{
				//echo'<pre>';print_r($ids['user_id']);exit;
				$notifications = Doctrine::getTable('AlbumNotifications')->getNotifications($ids['user_id']);
				//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
				if($notifications['sharedphotos_comments'] !="off")
				{
				$user_name = Doctrine::getTable('Users')->getEmail($ids['user_id']);
				//echo'<pre>';print_r($user_name);exit;
				$loginLink = $this->CONFIG->serverAddress.'home';
				$Subject = "comment on album notification from EmotionsMirror";
				$Message = 'Hello "'.$user_name['first_name'].'",<br/><br/>you have Comment Notification  in your emotionmirror account<br/><br/>
							<br/><br/><br/>Please click on the following link to login into your account<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
							Emirror team';
				global $serverAddress;
				sendMail($user_name['email'],$Subject,$Message,$_SESSION["sess_email"]);
				}
			}
			$result=Doctrine::getTable('EmComments')->updateEmCommentSendMailStatus($Photo_ids['id']);		
		}
		
	
	}
	
		
}	
